package com.sudo.api._admin;

import com.sudo.common.utils.JsonRes;
import com.sudo.common.utils.JsonUtil;
import com.sudo.dao.admin.entity.SysCpuLog;
import com.sudo.dao.admin.entity.SysInfo;
import com.sudo.dao.admin.entity.SysMemLog;
import com.sudo.dao.admin.model.vo.SysMemCpuVO;
import com.sudo.service.admin.service.SysCpuLogService;
import com.sudo.service.admin.service.SysInfoService;
import com.sudo.service.admin.service.SysMemLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 为道日损 2020-07-07 11:18
 * webSite：https://www.sudo.ren
 */
@Api(value = "监控信息",tags = {"监控信息相关的接口"})
@RestController
@RequestMapping("sysInfo")
public class SysInfoController {

    @Resource
    private SysInfoService sysInfoService;

    @Resource
    private SysMemLogService sysMemLogService;

    @Resource
    private SysCpuLogService sysCpuLogService;

    @ApiOperation(value = "查询节点的基础监控信息",notes = "查询节点的基础监控信息",httpMethod = "GET")
    @GetMapping("getAllBasicMonitorInfo")
    public JsonRes getAllBasicMonitorInfo(@RequestParam("serverNode") String serverNode) {
        List<SysInfo> sysInfoList = sysInfoService.getSysInfoByServer(serverNode);
        return JsonRes.ok(sysInfoList);
    }

    @ApiOperation(value = "获取cpu实时信息",notes = "获取cpu实时信息",httpMethod = "GET")
    @GetMapping("getCpuInfoActualTime")
    public JsonRes getCpuInfoActualTime(@RequestParam("serverNode") String serverNode) {
        List<SysCpuLog> sysCpuLogList = sysCpuLogService.getCpuLogListFromRedis(serverNode);
        return JsonRes.ok(sysCpuLogList);
    }

    @ApiOperation(value = "获取内存实时信息",notes = "获取内存实时信息",httpMethod = "GET")
    @GetMapping("getMemInfoActualTime")
    public JsonRes getMemInfoActualTime(@RequestParam("serverNode") String serverNode) {
        List<SysMemLog> sysMemLogList = sysMemLogService.getMemLogListFromRedis(serverNode);
        return JsonRes.ok(sysMemLogList);
    }

    @ApiOperation(value = "获取所有服务节点Cpu内存实时信息",notes = "获取内存实时信息",httpMethod = "GET")
    @GetMapping("getAllCpuMemInfoActualTime")
    public JsonRes getAllCpuMemInfoActualTime() {
        Map<String, Object>  sysMemLogList = sysMemLogService.getAllCpuMemInfoActualTime(0);
        return JsonRes.ok(sysMemLogList);
    }

    @ApiOperation(value = "获取服务节点Cpu内存实时信息",notes = "获取内存实时信息",httpMethod = "GET")
    @GetMapping("getCpuMemInfoActualTimeByServer")
    public JsonRes getCpuMemInfoActualTimeByServer(@RequestParam("server") String server) {
        String sysMemCpu = sysMemLogService.getCpuMemInfoActualTimeByServer(server,0);
        return JsonRes.ok(JsonUtil.jsonToPojo(sysMemCpu, SysMemCpuVO.class));
    }
}
